package demo04

/**
 * @author xiaoZhao
 * @date 2022/6/28
 * @describe
 */
object Test05_lambda {
  def main(args: Array[String]): Unit = {
    // 将整个函数作为一个值赋给变量中
    val fun= (name: String) => { println(name) }
    fun("wangwu")

    // 定义一个函数，以函数作为参数输入
    def f(func: String=> Unit): Unit={
      func("lisi")
    }
    f(fun)
    f((name: String) => { println(name) })
    f(name => println(name))
    f(println(_))
    f(println)


    // 匿名函数的简化原则


    // 定义一个二元运算函数，只操作1和2，具体的运算过程通过参数传入
    def dualFunctionOneAndTwo(func: (Int,Int) => Int ): Int = {
      func(1,2)
    }

    val add= (a: Int,b: Int) => a+b
    val minus= (a: Int,b: Int) => a-b

    println(dualFunctionOneAndTwo(add))
    println(dualFunctionOneAndTwo(minus))

    // 简化
    println(dualFunctionOneAndTwo((a: Int,b: Int) => a+b))
    println(dualFunctionOneAndTwo((a: Int,b: Int) => a-b))

    println(dualFunctionOneAndTwo((a,b) => a+b))
    println(dualFunctionOneAndTwo((a,b) => a-b))

    println(dualFunctionOneAndTwo(_+_))
  }
}
